<?php
/**
 * EzDialog 对话框
 * Ajax读取Url地址内容
 * <pre>
 * $this->widget('EzDialog', array(
 * 	'title' => 'EzDialog - ajax',
 * 	'url' => $this->createUrl('index/ajax'),
 * 	'width' => '800px',
 * 	'height' => '600px',
 * ));
 * </pre>
 * 直接载入HTML中元素内容
 * <pre>
 * $this->widget('EzDialog', array(
 * 	'title' => 'EzDialog - html element',
 * 	'target' => '#htmlElementId',
 * 	'widget' => '400px',
 * 	'height' => '280px',
 * ));
 * </pre>
 * 直接显示输出的内容
 * <pre>
 * $this->widget('EzDialog', array(
 * 	'title' => 'EzDialog - content',
 * 	'content' => 'Hello world',
 * 	'widget' => '280px',
 * 	'height' => '100px',
 * ));
 * </pre>
 * @author yangdongqi <yangdongqi@hayzone.com>
 * @package lib.widgets.ezdialog
 */
class CEzDialog extends CWidget {
	/**
	 * 皮肤文件
	 * 可以使用alias路径，如果为单个单词，则选择默认的皮肤文件
	 * @var string
	 */
	public $theme = 'default';

	/**
	 * 创建窗口返回的javascript变量名
	 * @var string
	 */
	public $jsVar;
	
	/**
	 * 窗口标题
	 * @var string
	 */
	public $title;
	
	/**
	 * 窗口内容URL
	 * 使用ajax读取数据
	 * @var string
	 */
	public $url;
	
	/**
	 * 加载HTML中某元素到窗口的内容
	 * @var string
	 */
	public $target;
	
	/**
	 * 最小高度CSS
	 * @var string
	 */
	public $minHeight;
	
	/**
	 * 最小宽度CSS
	 * @var string
	 */
	public $minWidth;
	
	/**
	 * 高度CSS
	 * @var string
	 */
	public $height;
	
	/**
	 * 宽度CSS
	 * @var string
	 */
	public $width;
	
	/**
	 * 关闭时询问
	 * @var string
	 */
	public $askOnClose;
	
	/**
	 * 创建后自动打开
	 * @var boolean
	 */
	public $autoOpen = false;
	
	/**
	 * 静态文件URL地址
	 * @var string
	 */
	protected $_assetUrl = null;
	
	/**
	 * 皮肤的物理文件路径
	 * @var string
	 */
	protected $_themePath = null;
	
	/**
	 * javascript 锁定输出，已经输出过就无需再次输出
	 * @var boolean
	 */
	protected static $_jsLock = false;
	
	public $content;
	
	/**
	 * 记录已发布的asset
	 * @var array
	 */
	protected static $_published = array();	
	
	public function run() {
		
		$theme = $this->themeUrl;
		$jsVar = $this->jsVar ? $this->jsVar.' = ' : '';
		$autoOpen = $this->autoOpen ? 1 : 0;
		$script = "
		<script type='text/javascript'>
		{$jsVar}$.EzDialog({
			'title': '{$this->title}',
			'minHeight': '{$this->minHeight}',
			'minWidth': '{$this->minWidth}',
			'askOnClose': '{$this->askOnClose}',
			'width': '{$this->width}',
			'height': '{$this->height}',
			'url': '{$this->url}',
			'target': '{$this->target}',
			'themeUrl': '{$theme}',
			'autoOpen': {$autoOpen}
		});
		</script>";
		
		if(!self::$_jsLock) {
			$jsUrl = Yii::app()->assetManager->publish(
				dirname(__FILE__) . '/jquery.ezdialog.js'
			);
			echo "<script src='{$jsUrl}'></script>";
			self::$_jsLock = true;
		}
		if(!isset(self::$_published[$this->theme])) {
			self::$_published[$this->theme] = true;
			echo $this->themeAsset;
		}
		
		echo $script;
	}
	
	/**
	 * 获取皮肤的URL网络地址
	 * @return string
	 */
	protected function getThemeUrl() {
		$file = $this->compileFile('theme.php');
		return Yii::app()->assetManager->publish($file);
	}
	
	/**
	 * 获取静态文件的URL网络地址
	 * @return string
	 */
	protected function getAssetUrl() {
		if(!$this->_assetUrl) {
			$themePath = $this->themePath;
			$this->_assetUrl = Yii::app()->assetManager->publish($themePath.'/assets');
		}
		return $this->_assetUrl;
	}
	
	/**
	 * 获取皮肤的物理文件地址
	 * @return string
	 */
	protected function getThemePath() {
		if(!$this->_themePath) {
			if(strpos($this->theme, '.')) {
				$path = Yii::getPathOfAlias($this->theme);
			} else {
				$path = dirname(__FILE__).'/themes/'.$this->theme;
			}
			$this->_themePath = $path;
		}
		return $this->_themePath;
	}
	
	/**
	 * 获取ASSET的内容
	 * @return string
	 */
	protected function getThemeAsset() {
		$file = $this->compileFile('asset.php');
		if(is_file($file)) {
			return file_get_contents($file);
		} else {
			return '';
		}
	}
	
	/**
	 * 获取皮肤的ID
	 * @return string
	 */
	protected function getThemeId() {
		$nodes = explode('.', $this->theme);
		return $nodes[count($nodes)-1];
	}
	
	/**
	 * 编译PHP文件
	 * @param string $type
	 */
	protected function compileFile($type) {
		$file = $this->themePath.'/'.$type;
		$newFilePath = Yii::getPathOfAlias('application.runtime.dialog_themes.'.$this->getThemeId());
		@mkdir($newFilePath, 0777, true);
		$content = $this->renderInternal($file, array('this'=>$this), true);
		$newFile = $newFilePath . '/' . $type;
		file_put_contents($newFile, $content);
		return $newFile;
	}
	

}

?>